package cn.User.controller;

import cn.controller.AopController;
import cn.User.service.UserService;
import cn.util.aop.Authority;
import cn.util.json.JacksonUtil;
import common.constant.ServiceResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserConterller {

    private Logger logger=LoggerFactory.getLogger(AopController.class);

    @Autowired
    UserService userService;


    /**
     * 记录用户浏览记录
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("History.json")
    @Authority()
    public Object  setUserHistory(@RequestBody String json, Model model) {
        logger.info("访问地址===History.json;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //商品数据
            result.put("Hello",userService.setUserHistory(map));
            //商品类型
            //返回模板
            map.put("path","HelloWord!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);

    }

    /**
     * 获取用户信息
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("getuserinfo.json")
    @Authority()
    public Object  getUserList(@RequestBody String json, Model model)  {
        logger.info("访问地址===getuserinfo.json;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //商品数据
            result.put("Hello",userService.getuserinfo(map));
            //商品类型
            //返回模板
            map.put("path","HelloWord!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);

    }


    /**
     * 登陆接口:1.账号密码登陆 2.手机号登陆 3.QQ登陆 4.微信登陆
     *
     * 映射地址:1.普通登陆，2.第三方平台登陆
     */
    @Authority()
    @ResponseBody
    @RequestMapping(value = {"/login.do","/successlogin.do"})
    public Object SuccessLogin(@RequestBody String json, Model model,@RequestParam(value = "type",required = false) Integer TYPE, HttpServletRequest request){
        logger.info("访问地址===successlogin.do;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            if(TYPE!=null){
                map.put("type",TYPE);
            }
            result=userService.SuccessLogin(map,request);
            result.put("url",map.get("url"));
            map.put("resultype",result.get("resultype"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);
    }

    /**
     * 退出接口
     *
     *
     */
    @Authority()
    @ResponseBody
    @RequestMapping(value ="/logout.do")
    public Object logout(@RequestBody String json, Model model){
        logger.info("访问地址===logout.do;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put("path","redirect:/");
            result=userService.logout(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);
    }

    /**
     * 是否登陆
     *
     *
     */
    @Authority()
    @ResponseBody
    @RequestMapping(value ="/islogin.do")
    public Object islogin(@RequestBody String json, Model model){
        logger.info("访问地址===islogin.do;访问参数==="+json);
        //传过来的数据
        Map<String,Object> map= null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","json");
            result=userService.islogin(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model,map,result);
    }







    //    @Authority()
//    @RequestMapping("/login")
//    public String Login(@RequestParam("User")String User, @RequestParam("pwd")String pwd, HttpServletRequest request){
//        Subject subject=SecurityUtils.getSubject();
//        UsernamePasswordToken token=new UsernamePasswordToken(User, pwd);
//        System.out.println("欢迎登陆"+request.getSession().getId());
//        try {
//            subject.login(token);
//            Session session = subject.getSession();
//            System.out.println("sessionId:" + session.getId());
//            System.out.println("sessionHost:" + session.getHost());
//            System.out.println("sessionTimeout:" + session.getTimeout());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "login";
//    }
//
    @RequestMapping("/index")
    public String admin(){
        System.out.println("欢迎你");
        return "admin";
    }


    @RequestMapping("/admin")
    public String teacher(){

        System.out.println("欢迎你管理员");

        return "teacher";
    }

//try {
////            System.out.println(SecurityUtils.getSubject());
//        SecurityUtils.getSubject().login(token);
//    } catch (UnknownAccountException ex) {
//        return "用户不存在或者密码错误！";
//    } catch (IncorrectCredentialsException ex) {
//        return "用户不存在或者密码错误！";
//    }catch (ExcessiveAttemptsException ex) {
//        return "账号或密码错误次数过多,请稍后重试！";
//    } catch (AuthenticationException ex) {
//        return ex.getMessage(); // 自定义报错信息
//    } catch (Exception ex) {
//        ex.printStackTrace();
//        return "内部错误，请重试！";
//    }

}
